package com.jms.rabbitmq.config;

import com.jms.rabbitmq.constant.RabbitMQConstant;
import com.jms.rabbitmq.constant.Topic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * create by yongli on 2021-06-13 00:29
 */

@Configuration
@ConditionalOnProperty(name = "config.rabbitmq.enable", havingValue = "true")
public class RabbitMQConfig {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());


//    这里会报错
//    @Autowired
//    private RabbitTemplate rabbitTemplate;
//
//    public RabbitTemplate rabbitTemplate() {
//        return this.rabbitTemplate;
//    }

    @Bean
    public MessageConverter messageConverter() {
        return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter());
    }

    /**
     * 每次请求时，都会创建一个新的
     *
     * @return
     */
    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplate(
            ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        return rabbitTemplate;
    }


    @Bean
    public TopicExchange topicExchange() {
        logger.info(RabbitMQConstant.EXCHANGE_NAME_BPM);
        // 持久化该exhange，然后如果没有队列绑定，则自动删除该exchange
        return new TopicExchange(RabbitMQConstant.EXCHANGE_NAME_BPM, true, true);
    }

    @Bean
    public Queue helloQueue() {
        return new Queue(Topic.HELLO, true);
    }

    @Bean
    public Queue faultQueue() {
        return new Queue(Topic.FAULT, true);
    }

    @Bean
    public Queue bigEventQueue() {
        return new Queue(Topic.BIG_EVENT);
    }

    // bean修饰的方法，能够正确注入对象
    @Bean
    public Binding bindingMmCheck(Queue faultQueue, TopicExchange exchangeBPM) {
        // 绑定的过程
        return BindingBuilder.bind(faultQueue).to(exchangeBPM).with(RabbitMQConstant.ROUTING_KEY_BPM);
    }

    @Bean
    public Binding bindingBigEvent(Queue bigEventQueue, TopicExchange exchangeBpm) {
        return BindingBuilder.bind(bigEventQueue).to(exchangeBpm).with(RabbitMQConstant.ROUTING_KEY_BPM);
    }

    @Bean
    public Binding bindingHello(Queue helloQueue, TopicExchange exchangeBpm) {
        return BindingBuilder.bind(helloQueue).to(exchangeBpm).with(RabbitMQConstant.ROUTING_KEY_BPM);
    }
}
